﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Text;

namespace MoneyClass
{
    public class Image
    {
        /// <summary>
        /// 数据源是PieChartValue对象的饼状图
        /// </summary>
        /// <param name="title">饼状图大标题</param>
        /// <param name="subTitle">饼状图小标题</param>
        /// <param name="width">图宽</param>
        /// <param name="height">图高</param>
        /// <param name="Mydata">PieChartValue对象</param>
        /// <returns>饼状图路径</returns>
        public static Bitmap Render(Color backColor, string title, string subTitle, int width, int height, PieChartValue[] Mydata)
        {
            const int SIDE_LENGTH = 500;
            const int PIE_DIAMETER = 200;
            //DataTable dt = DataTable;
             
            //通过输入参数，取得饼图中的总基数 
            float sumData = 0;
            for (int i = 0; i < Mydata.Length; i++)
            {
                sumData += Convert.ToSingle(Mydata[i].MValue);
            }
            //产生一个image对象，并由此产生一个Graphics对象 
            Bitmap bm = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bm);
            //设置对象g的属性 
            g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / PIE_DIAMETER);
            g.SmoothingMode = SmoothingMode.Default;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;

            //画布和边的设定 
            g.Clear(backColor);
            //g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
            //画饼图标题 
            //g.DrawString(title, new Font("Tahoma", 14), Brushes.Black, new PointF(5, 5));
            //画饼图的图例 
            //g.DrawString(subTitle, new Font("Tahoma", 12), Brushes.Black, new PointF(7, 35));
            //画饼图 
            float curAngle = 0;
            float totalAngle = 0;
            for (int j = 0; j < Mydata.Length; j++)
            {
                curAngle = Convert.ToSingle(Mydata[j].MValue) / sumData * 360;

                g.FillPie(new SolidBrush(ChartUtil.GetChartItemColor(j)), 0, 0, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
                g.DrawPie(Pens.Black, 0,0, PIE_DIAMETER, PIE_DIAMETER, totalAngle, curAngle);
                totalAngle += curAngle;
            }
            //画图例框及其文字 
           //g.DrawRectangle(Pens.Black, height + 5, 0, width - height - 5, height);
            g.DrawString("图表说明", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(height, 0));

            //画图例各项 
            PointF boxOrigin = new PointF(height + 15, 30);
            PointF textOrigin = new PointF(height + 35, 26);
            float percent = 0;
            for (int k = 0; k < Mydata.Length; k++)
            {
                g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(k)), boxOrigin.X, boxOrigin.Y, 20, 10);
                g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 20, 10);
                percent = Convert.ToSingle(Mydata[k].MValue) / sumData * 100;
                g.DrawString(Mydata[k].MValue.ToString() + " - " + Mydata[k].Name.ToString() + " (" + percent.ToString("0") + "%)", new Font("Tahoma", 12), Brushes.Black, textOrigin);
                boxOrigin.Y += 20;
                textOrigin.Y += 20;
            }
            //回收资源 
            g.Dispose();
            return bm;
        }

        /// <summary>
        /// 数据源是BarChartValue对象的柱状图
        /// </summary>
        /// <param name="title">大标题</param>
        /// <param name="subTitle">小标题</param>
        /// <param name="width">图宽</param>
        /// <param name="height">图高</param>
        /// <param name="Mydata">BarChartValue对象</param>
        /// <returns>柱状图路径</returns>
        public static Bitmap Render(string title, string subTitle, int width, int height, BarChartValue[] Mydata)
        {
            const int SIDE_LENGTH = 400;
            const int CHART_TOP = 75;
            const int CHART_HEIGHT = 200;
            const int CHART_LEFT = 50;
            const int CHART_WIDTH = 300;

            //计算最高的点 
            float highPoint = 0;
            for (int i = 0; i < Mydata.Length; i++)
            {
                if (highPoint < Convert.ToSingle(Mydata[i].MValue))
                {
                    highPoint = Convert.ToSingle(Mydata[i].MValue);
                }
            }
            //建立一个Graphics对象实例 
            Bitmap bm = new Bitmap(width, height);

            Graphics g = Graphics.FromImage(bm);
            //设置条图图形和文字属性 
            g.ScaleTransform((Convert.ToSingle(width)) / SIDE_LENGTH, (Convert.ToSingle(height)) / SIDE_LENGTH);
            g.SmoothingMode = SmoothingMode.Default;
            g.TextRenderingHint = TextRenderingHint.AntiAlias;

            //设定画布和边 
            g.Clear(Color.White);
            g.DrawRectangle(Pens.Black, 0, 0, SIDE_LENGTH - 1, SIDE_LENGTH - 1);
            //画大标题 
            g.DrawString(title, new Font("Tahoma", 14), Brushes.Black, new PointF(5, 5));
            //画小标题 
            g.DrawString(subTitle, new Font("Tahoma", 12), Brushes.Black, new PointF(7, 35));
            //画条形图 
            float barWidth = CHART_WIDTH / (Mydata.Length * 2);
            PointF barOrigin = new PointF(CHART_LEFT + (barWidth / 2), 0);
            float barHeight = Mydata.Length;
            for (int i = 0; i < Mydata.Length; i++)
            {
                barHeight = Convert.ToSingle(Mydata[i].MValue) * 200 / highPoint * 1;
                barOrigin.Y = CHART_TOP + CHART_HEIGHT - barHeight;
                g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), barOrigin.X, barOrigin.Y, barWidth, barHeight);



                g.DrawString(Mydata[i].Name, new Font("Tahoma", 3), Brushes.Black, new PointF(barOrigin.X - 3, 277));



                barOrigin.X = barOrigin.X + (barWidth * 2);
            }
            //设置边 
            g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT));
            g.DrawLine(new Pen(Color.Black, 2), new Point(CHART_LEFT, CHART_TOP + CHART_HEIGHT), new Point(CHART_LEFT + CHART_WIDTH, CHART_TOP + CHART_HEIGHT));
            //画图例框和文字


            //g.DrawRectangle(Pens.Black, 200, 300, 199, 99);
            //g.DrawString("图表说明", new Font("Tahoma", 12, FontStyle.Bold), Brushes.Black, new PointF(200, 300));

            g.DrawRectangle(new Pen(Color.Black, 1), 10, 290, 299, 110);
            g.DrawString("图表说明", new Font("Tahoma", 10, FontStyle.Bold), Brushes.Black, new PointF(11, 290));

            //画图例 
            PointF boxOrigin = new PointF(10, 310);
            PointF textOrigin = new PointF(35, 306);
            for (int i = 0; i < Mydata.Length; i++)
            {
                g.FillRectangle(new SolidBrush(ChartUtil.GetChartItemColor(i)), boxOrigin.X, boxOrigin.Y, 20, 10);
                g.DrawRectangle(Pens.Black, boxOrigin.X, boxOrigin.Y, 2, 1);
                g.DrawString(Mydata[i].Name.ToString() + " - " + Mydata[i].MValue.ToString(), new Font("Tahoma", 10), Brushes.Black, textOrigin);
                if (i < 5)
                {
                    boxOrigin.Y += 15;
                    textOrigin.Y += 15;
                }
                else if (i == 5)
                {
                    boxOrigin.X = 150;
                    boxOrigin.Y = 310;
                    textOrigin.X = 176;
                    textOrigin.Y = 306;

                }
                else
                {
                    boxOrigin.Y += 15;
                    textOrigin.Y += 15;
                }

            }
            //输出图形 
            g.Dispose();
            return bm;

        }
    }

    /// <summary>
    /// 选择颜色
    /// </summary>
    public class ChartUtil
    {
        public ChartUtil()
        {
        }

        public static Color GetChartItemColor(int itemIndex)
        {
            Color selectedColor;
            switch (itemIndex)
            {
                case 0:
                    selectedColor = Color.AliceBlue;
                    break;
                case 1:
                    selectedColor = Color.Red;
                    break;
                case 2:
                    selectedColor = Color.Yellow;
                    break;
                case 3:
                    selectedColor = Color.AntiqueWhite;
                    break;
                case 4:
                    selectedColor = Color.Aqua;
                    break;
                case 5:
                    selectedColor = Color.Aquamarine;
                    break;
                case 6:
                    selectedColor = Color.Azure;
                    break;
                case 7:
                    selectedColor = Color.Beige;
                    break;
                case 8:
                    selectedColor = Color.Black;
                    break;
                case 9:
                    selectedColor = Color.Brown;
                    break;
                case 10:
                    selectedColor = Color.Coral;
                    break;
                case 11:
                    selectedColor = Color.DarkCyan;
                    break;
                case 12:
                    selectedColor = Color.DarkOrange;
                    break;
                default:
                    selectedColor = Color.DarkViolet;
                    break;
            }
            return selectedColor;
        }
    }

    /// <summary>
    /// 饼状图的数据对象
    /// </summary>
    public class PieChartValue
    {
        public PieChartValue()
        {

        }
        public PieChartValue(string MyName, int Myvalue)
        {
            Name = MyName;
            MValue = Myvalue;
        }
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        private int mvalue;
        public int MValue
        {
            get
            {
                return mvalue;
            }
            set
            {
                mvalue = value;
            }
        }
    }

    /// <summary>
    /// 柱状图的数据对象
    /// </summary>
    public class BarChartValue
    {
        public BarChartValue()
        {

        }
        public BarChartValue(string MyName, int Myvalue)
        {
            Name = MyName;
            MValue = Myvalue;
        }
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        private int mvalue;
        public int MValue
        {
            get
            {
                return mvalue;
            }
            set
            {
                mvalue = value;
            }
        }
    }
}